let app= getApp()
import { login } from '../../api/getData.js'
Page({

  data: {
    accountName: '',
    password: '',
    noticeShow: false,
    noticeText: ''
  },

  onLoad(){
    app.globalData.result = null
  },
  onShow(){
    clearTimeout(app.globalData.timer)
  },

  // get-input-context
  getInp(e){
    let obj = {};
    obj[e.target.dataset.sign] = e.detail.value
    obj.noticeShow = false
    this.setData(obj)
  },
  // login-botton
  submitLogin(){
    let accountName = this.data.accountName.trim(),
      password = this.data.password.trim()

    if (!accountName || !password){
      this.setData({
        noticeText: '请先输入用户名或密码！',
        noticeShow: true
      })
      return
    }

    // 防止重复请求
    if (app.globalData.requestBool) return
    app.globalData.requestBool = true

    login(accountName, password)
      .then( res => {
        
        if (res.resCode === 'SUCCESS' ){
          res = res.result
          wx.setStorageSync('token', res.token)
          wx.setStorageSync('username', res.userName)
          wx.setStorageSync('moduleList', res.functionModule.moduleList)
          wx.setStorageSync('moduleConfig', res.functionModule.moduleConfig)
          wx.setStorageSync('canRequest', true)
          wx.showToast({
            title: '登录成功！',
            duration: 500
          })
          
          setTimeout(()=>{
            wx.reLaunch({
              url: '/pages/totalMsg/totalMsg',
            })
          }, 500)
        } else {
          this.setData({
            noticeText: '用户名或密码错误！请重新输入。',
            noticeShow: true
          })
        }
      })
      // .catch( msg => {
      //   console.log('catch')
      //   wx.showToast({
      //     icon: 'none',
      //     title: '请求失败！',
      //   })
      // })
  }
})